package iscript.uiFramework.components.container 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import iscript.uiFramework.components.layout.EmptyLayout;
	import iscript.uiFramework.components.layout.FlowLayout;
	import iscript.uiFramework.components.layout.ILayoutManager;
	import iscript.uiFramework.components.layout.LayoutMargin;
	import iscript.uiFramework.components.UIComponent;
	import iscript.uiFramework.event.ContainerEvent;
	
	
		
	//[Event(name="SourceUpdate",type="iscript.uiFramework.event.ContainerEvent")]
	public class Container extends UIComponent implements IContainer
	{
	 
		private var _layout:ILayoutManager;
		
		
		/**
		 *  没有自动遮罩的容器
		 */
		public function Container(preferWidth:Number=0,preferHeight:Number=0) 
		{
			super(preferWidth,preferHeight);
		}
		
		override protected function initialize():void 
		{
			_layout = new EmptyLayout();			
			_layout.container = this;
			_layout.setPreferWidth(_preferWidth);
			super.initialize();
		}
		
	 
		override public function set width(value:Number):void 
		{ 
			_preferWidth = value;
			
				_layout.setPreferWidth(_preferWidth);
			//
			if (IsRender && !(_layout is FlowLayout)) {
				  _layout.updateAlign();
				}
			super.width = value;		 
		}
	
		override public function get width():Number 
		{ 
		 	if (_layout is EmptyLayout) return _layout.width;
			return _preferWidth;
		}
		
		override public function set height(value:Number):void 
		{ 
			_preferHeight = value;
			_layout.setPreferHeight(_preferHeight);
			super.height = value;
		
		}
		
		override public function get height():Number 
		{ 
		 	if (_layout is EmptyLayout) return _layout.height;
			return _preferHeight;
		}
		public function sourceUpdate():void {
			if (_layout) {
				 _layout.updateAlign();
			   }
			//do anything
			/*if(!Disposed)
			 this.dispatchEvent(new ContainerEvent(ContainerEvent.SOURCEUPDATE));*/
			}
	    public function get container():DisplayObjectContainer {
			return  this;
			}
				
		public function setLayout(layout:ILayoutManager):void {	
			 _layout = layout;
			 layout.container = this;
			 if (layout is FlowLayout)
			 {
				 _preferWidth = 0;
				 _preferHeight = 0;
			 }
		}
        		
		public function getLayout():ILayoutManager {
			return _layout;
		}
		
		/**
		 * 添加显示元素
		 * @param	target
		 */
		public function append(target:DisplayObject):void {
			if (_layout)
			  _layout.append(target);
			}
		 
		/**
		 * 移除显示元素
		 * @param	target
		 */
		public function remove(target:DisplayObject):void {
			if(_layout)
			  _layout.remove(target);
			}
			
		public function clear():void {
			  if(_layout){
			  _layout.clear();
			  sourceUpdate();
			  }
			}	
		
			
		override public function render(container:DisplayObjectContainer = null):void 
		{
			sourceUpdate();
			super.render(container);
		}
		
	}

}